package com.riant.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.riant.demo.entity.User;
import com.riant.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author riant
 * @since 2021-07-01
 */
@RestController
@RequestMapping("/user")
public class UserContoller {
    @Autowired
    private UserService userService;

    @RequestMapping(path = "/all",method = RequestMethod.GET)
    public List<User> getAll(){
        return  userService.list();
    }

    @RequestMapping(path = "/{id}",method = RequestMethod.GET)
    public User getOne(@PathVariable int id){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return  userService.getOne(queryWrapper);
    }

    @RequestMapping(path = "/insert",method = RequestMethod.POST)
    public Boolean addUser(@RequestBody User user){
        Boolean result=userService.save(user);
//        if(result){
//            return result;
//        }
        return  result;
    }

    @RequestMapping(path = "/update",method = RequestMethod.POST)
    public Boolean updateUser(@RequestBody  User user){
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.eq("id",user.getId());
        //userUpdateWrapper.setSql("id="+user.getId());
       // userUpdateWrapper.setEntity(user);
        Boolean result=userService.update(user,userUpdateWrapper);
        return  result;
    }
    @RequestMapping(path = "/updateUser",method = RequestMethod.POST)
    public Boolean updateUserById(@RequestBody  User user){
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        Boolean result=userService.updateById(user);
        return  result;
    }

    @RequestMapping(path = "/remove/{id}",method = RequestMethod.GET)
    public Boolean removeUser(@PathVariable int id){

        Boolean result=userService.removeById(id);
        return  result;
    }

}

